package com.mlh.furn.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mlh.furn.bean.Furn;
import com.mlh.furn.bean.Msg;
import com.mlh.furn.service.FurnService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FurnController {
	@Resource
	FurnService furnService;

	//@RequestBody:使用SpringMVC的@RequestBody将客户端提交的json数据，封装成JavaBean
	//@ResponseBody:服务器返回的数据格式是按照json来返回的(底层是按照http协议进行协商)
	@PostMapping("/save")
	@ResponseBody
	public Msg save(@Validated @RequestBody Furn furn, Errors errors) {//将前端发送的 json 数据,转成 JavaBean 数据
		Map<String,Object> map = new HashMap<>();
		List<FieldError> fieldErrors = errors.getFieldErrors();
		for (FieldError fieldError : fieldErrors) {
			map.put(fieldError.getField(),fieldError.getDefaultMessage());
		}
		if(map.isEmpty()){//map为空 则没有错误产生
			furnService.save(furn);
			return Msg.success();
		}else{
			return Msg.fail().add("errorMsg",map);//有错误产生则返回错误信息
		}
	}

	@RequestMapping("/furns")
	@ResponseBody
	public Msg listFurns(){
		List<Furn> furnList=furnService.findAll();
		//将数据封装到Msg对象中去
		Msg msg = Msg.success();
		msg.add("furnList",furnList);
		return msg;
	}

	@PutMapping("/update")
	@ResponseBody
	public Msg update(@RequestBody Furn furn) {
		furnService.update(furn);
		return Msg.success();
	}
	@DeleteMapping("/del/{id}")
	@ResponseBody
	public Msg del(@PathVariable Integer id){
		furnService.del(id);
		return Msg.success();
	}

	@RequestMapping("/findOne/{id}")
	@ResponseBody
	public Msg findOne(@PathVariable Integer id){
		Furn one = furnService.findOne(id);
		Msg msg=Msg.success();
		msg.add("furn",one);
		return msg;
	}

	@RequestMapping("/furnsByPage")
	@ResponseBody
	public Msg furnsByPage(@RequestParam(defaultValue = "1") Integer pageNum,
	                      @RequestParam (defaultValue = "5")Integer pageSize){
		//设置分页参数
		//1.调用findAll是完成查询，底层会进行物理分页，而不是逻辑分页
		//2.会根据分页参数来计算limit ?，?，在发出SQL语句时，会带Limit
		PageHelper.startPage(pageNum, pageSize);
		List<Furn> furnList = furnService.findAll();
		//将分页查询的结果，封装到PageInfo
		//PageInfo对象包含了分页的各个信息,比如当前页面pageNum，共有多少记录
		PageInfo pageInfo = new PageInfo(furnList, pageSize);
		return Msg.success().add("pageInfo",pageInfo);
	}

	@RequestMapping("/furnsByConditionPage")
	@ResponseBody
	public Msg furnsByConditionPage(
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5")Integer pageSize,
			@RequestParam(defaultValue = "")String search){
		PageHelper.startPage(pageNum, pageSize);
		List<Furn> furnList = furnService.findByCondition(search);
		PageInfo pageInfo = new PageInfo(furnList, pageSize);
		return Msg.success().add("pageInfo",pageInfo);
	}

}
